import { useMessage } from '@/hooks'
import { defineStore } from 'pinia'
import { casinoCollectGame, getGameFavoriteState } from '@/api/casino'
import { useAccountStore } from './account'
import { t } from '@/utils'

const typeKv: any = {
	baccarat: 'baccarat',
	blackjack: 'blackjack',
	sicbo: 'baccarat_sicbo',
	roulette: 'roulette',
	dragontiger: 'dragon_tiger',
	poker: 'poker',
	gameshow: 'game_shows'
}

async function getFavoriteState(val: any) {
	const accountStore = useAccountStore()
	if (!accountStore.isLogin) {
		return false
	}
	if (val.status === 1 && !Object.prototype.hasOwnProperty.call(val, 'favoriteState')) {
		try {
			const { data } = await getGameFavoriteState({ eid: val.eid })
			return data
		} catch (error) {
			console.error(error)
		}
	}
	return false
}
export const useCasinoStore = defineStore('casino', {
	state: () => ({
		bigWinsList: [] as any[],
		providerList: [] as any[],
		recommendsList: [] as any[],
		casinosList: [] as any[],
		liveBigDataList: [] as any[],
		currentRecomentedTab: 1,
		gameDetail: {} as any,
		currentTabType: '' as string,
		lobbyTabList: [] as any[],
		bigWinnerList: [] as any[],
		sideMenuCasinoList: [] as any[],
		allBetList: [] as any[],
		isWeeklyRaceChange: false,
		weeklyRaceData: {} as any
	}),
	actions: {
		setLobbyTabList(val: any[]) {
			this.lobbyTabList = val
		},
		setAllBetList(val: any[]) {
			this.allBetList.unshift(val)
			this.allBetList.pop()
		},
		setBigWinnerList(val: any[]) {
			this.bigWinnerList = val
		},
		setSideMenuCasinoList(val: any[]) {
			this.sideMenuCasinoList = val
		},
		setTabType(val: string) {
			this.currentTabType = val
		},
		setLiveBigWinnersList(val: any[]) {
			this.liveBigDataList = val
		},
		setProviderList(val: any[]) {
			this.providerList = val
		},
		setRecommendsList(val: any[]) {
			this.recommendsList = val
		},
		setCasinosList(val: any[]) {
			this.casinosList = val
		},
		setGameDetail(val: any) {
			this.gameDetail = val
		},
		setWeeklyRace(val: any) {
			this.isWeeklyRaceChange = !this.isWeeklyRaceChange
			this.weeklyRaceData = val
		},
		updateAllListStatus(val: any) {
			console.log(val)
			val.data.forEach((newItem: any) => {
				newItem.eid = newItem.id
				newItem.promotion = newItem.origin.promotion
				if (val.action === 'add') {
					newItem.favoriteState = false
					// liveUpdateRefsData(newItem, val.action)
					this.addItem(newItem)
				}
				if (val.action === 'update') {
					// checkGameFavoriteState(newItem, val.action)
					this.updateItem(newItem)
				}
				if (val.action === 'delete') {
					// liveUpdateRefsData(newItem, val.action)
					this.deleteItem(newItem)
				}
			})
		},
		addItem(val: any) {
			console.log(val)
			if (val.promotion === 1) {
				this.recommendsList.push(val)
			}
			// 判断是否添加all game
			if (
				this.currentTabType === 'all' ||
				(this.currentTabType === 'promotion' && val.promotion === 1) ||
				typeKv[this.currentTabType] === val.gameType
			) {
				this.casinosList.push(val)
			}
		},
		async updateItem(val: any) {
			console.log(val)
			const accountStore = useAccountStore()
			if (!Object.prototype.hasOwnProperty.call(val, 'favoriteState') || !accountStore.isLogin) {
				const favoriteState = await getFavoriteState(val)
				val.favoriteState = favoriteState
				this.updateAction(val)
			}
		},
		updateAction(val: any) {
			const recommendIndex = this.recommendsList.findIndex((item) => item.eid === val.eid)
			if (recommendIndex > -1) {
				this.recommendsList[recommendIndex] = val
			}

			const gamesIndex = this.casinosList.findIndex((item) => item.eid === val.eid)
			if (gamesIndex > -1) {
				this.casinosList[gamesIndex] = val
			}
			if (val.id === this.gameDetail.eid) {
				this.gameDetail = val
			}
		},
		deleteItem(val: any) {
			const casinoItem = this.casinosList.find((item) => item.eid === val.id)
			if (casinoItem) {
				casinoItem.status = 0
			}
			const recommendItem = this.recommendsList.find((item) => item.eid === val.id)
			if (recommendItem) {
				recommendItem.status = 0
			}
			if (val.id === this.gameDetail.eid) {
				this.gameDetail.status = 0
			}
		},
		setLiveWins(val: any) {
			if (val.type === -1) {
				this.bigWinsList = val.data
			} else if (val.type === this.currentRecomentedTab) {
				// this.bigWinsList = val.data[curLanguage()]
				if (this.bigWinsList.length && val.data.length) {
					const existingWinnersSet = new Set(
						this.bigWinsList.map((winner: any) => `${winner.timestamp}-${winner.eid}`)
					)
					const newWinners = val.data
						.filter((winner: any) => !existingWinnersSet.has(`${winner.timestamp}-${winner.eid}`))
						.slice(0, 20 - this.bigWinsList.length)
					this.bigWinsList = [...this.bigWinsList, ...newWinners].slice(0, 20)
				} else {
					this.bigWinsList = val.data.slice(0, 20)
				}
			}
		},
		addLiveBigWin(val: any) {
			if (this.liveBigDataList) {
				this.liveBigDataList.unshift(val)
				setTimeout(() => {
					if (this.liveBigDataList && this.liveBigDataList.length > 10) {
						this.liveBigDataList.pop()
					}
				}, 1000)
			}
		},
		async cancelCollectGame(parmas: any) {
			const message = useMessage()
			try {
				await casinoCollectGame(parmas)
				this.updateCollectedItemStatus({
					isCollect: false,
					id: parmas.eid
				})
			} catch (err: any) {
				message.error(err)
			}
		},
		async collectGame(parmas: any) {
			try {
				const message = useMessage()
				await casinoCollectGame(parmas)
				message.success(t('slotsall.collectSuccess'))
				this.updateCollectedItemStatus({
					isCollect: true,
					id: parmas.eid,
					isFavorite: parmas.isFavorite
				})
			} catch (err) {
				console.log(err)
			}
		},
		// updateItemStatus(val: any) {
		// 	this.recommendsList.forEach((item) => {
		// 		if (item.gameId === val.gameId) {
		// 			item = val
		// 		}
		// 	})
		// 	this.bigWinsList.forEach((item) => {
		// 		if (item.gameId === val.gameId) {
		// 			item = val
		// 		}
		// 	})
		// 	this.casinosList.forEach((item) => {
		// 		if (item.gameId === val.gameId) {
		// 			item = val
		// 		}
		// 	})
		// },
		updateCollectedItemStatus(val: any) {
			const casinoItem = this.casinosList.find((item) => item.eid === val.id)
			if (casinoItem) {
				casinoItem.favoriteState = val.isCollect
			}
			if (val.isFavorite) {
				this.casinosList.map((item: any, index: number) => {
					if (item.eid === val.id) {
						this.casinosList.unshift(this.casinosList.splice(index, 1)[0])
					}
				})
			}
			const recommendItem = this.recommendsList.find((item) => item.eid === val.id)
			if (recommendItem) {
				recommendItem.favoriteState = val.isCollect
			}
			if (val.id === this.gameDetail.eid) {
				this.gameDetail.collectState = val.isCollect
			}
		}
	},
	getters: {
		firstLiverBigWinner: (state) => state.liveBigDataList[0]
	},
	persist: true
})
